package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.BusModelPartsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BusModelAccessories;
import com.ruoyi.system.service.IBusModelAccessoriesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 领料列表Controller
 * 
 * @author ruoyi
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/system/accessories")
public class BusModelAccessoriesController extends BaseController
{
    @Autowired
    private IBusModelAccessoriesService busModelAccessoriesService;
    @Autowired
    private BusModelPartsService busModelPartsService;

    /**
     * 查询领料列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusModelAccessories busModelAccessories)
    {
        startPage();
        List<BusModelAccessories> list = busModelAccessoriesService.selectBusModelAccessoriesList(busModelAccessories);
        for (BusModelAccessories busModelAccessories1:list) {
            busModelAccessories1.setBusModelList(busModelPartsService.getBusModelByPartsCode(busModelAccessories1.getPartsCode()));
        }
        return getDataTable(list);
    }

    /**
     * 导出领料列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:export')")
    @Log(title = "领料列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusModelAccessories busModelAccessories)
    {
        List<BusModelAccessories> list = busModelAccessoriesService.selectBusModelAccessoriesList(busModelAccessories);
        ExcelUtil<BusModelAccessories> util = new ExcelUtil<BusModelAccessories>(BusModelAccessories.class);
        util.exportExcel(response, list, "领料列表数据");
    }

    /**
     * 获取领料列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
         BusModelAccessories busModelAccessories = busModelAccessoriesService.selectBusModelAccessoriesById(id);
        busModelAccessories.setBusModelList(busModelPartsService.getBusModelByPartsCode(busModelAccessories.getPartsCode()));
        return success(busModelAccessories);
    }

    /**
     * 新增领料列表
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:add')")
    @Log(title = "领料列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusModelAccessories busModelAccessories)
    {
         int count = busModelAccessoriesService.insertBusModelAccessories(busModelAccessories);
        List<String> busModelList = busModelAccessories.getBusModelList();
        for (String busModel:busModelList) {
            busModelPartsService.insertModelByArray(busModel, busModelAccessories.getPartsCode());
        }
        return toAjax(count);
    }

    /**
     * 修改领料列表
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:edit')")
    @Log(title = "领料列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusModelAccessories busModelAccessories)
    {
        int count = busModelAccessoriesService.updateBusModelAccessories(busModelAccessories);
        busModelPartsService.deleteModelByArray(busModelAccessories.getPartsCode());
        List<String> busModelList = busModelAccessories.getBusModelList();
        for (String busModel:busModelList) {
               busModelPartsService.insertModelByArray(busModel, busModelAccessories.getPartsCode());
        }
        return toAjax(count);
    }

    /**
     * 删除领料列表
     */
    @PreAuthorize("@ss.hasPermi('system:accessories:remove')")
    @Log(title = "领料列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busModelAccessoriesService.deleteBusModelAccessoriesByIds(ids));
    }

    /**
     * 主表信息插入子表
     */
    @GetMapping("/mainToChild")
    public AjaxResult mainToChild()
    {
        List<String> countPartsCode = busModelAccessoriesService.getCountPartsCode();
        for (String partsCode:countPartsCode) {
             List<String> busModelByPartsCode = busModelAccessoriesService.getBusModelByPartsCode(partsCode);
            for (String busModel:busModelByPartsCode) {
                busModelPartsService.insertBusModelByPartsCode(busModel,partsCode);
            }
        }
        return toAjax(true);
    }



}
